package net.dreamlu.qa.controller;

import net.dreamlu.qa.model.User;
import net.dreamlu.util.PageUtil;

import com.jfinal.core.Controller;
import com.jfinal.kit.StringKit;

/**
 * 用户中心
 * @author chunmeng.lu
 *
 */
public class UController extends Controller{
	
	// 根据用户id 获取
	public void index(){
		User user = User.dao.findById(getParaToInt()); 
		if(StringKit.notNull(user)){
			setAttr("user", user);
			// 查询用户的问题 1|6 分页 
			setAttr("uQuestList", user.getQuestPage(6));
			// 查询用户的回答 1|6 分页
			setAttr("uAnswerList", user.getAnswersPage(6));
			// 获取用户的问题数
			setAttr("uQuestCount", user.getUQuestCount(user));
			// 获取用户的回答数
			setAttr("uAnswerCount", user.getUAnswerCount(user));
			setAttr("uTimeGo", user.getTimeGo());
			render("/solution/user_info.jsp");
		}else{
			render("/error/error.jsp");
		}
	} 
	
	// 用户的答案
	public void answers(){
		User user = User.dao.findById(getParaToInt()); 
		int pageNum = getPara("page") == null ? 1 : getParaToInt("page");
		if(StringKit.notNull(user)){
			Integer uAnswerCount = User.dao.getUAnswerCount(user);
			PageUtil page = new PageUtil(pageNum, uAnswerCount, "u/answers/" + getPara());
			setAttr("user", user);
			// 查询用户的回答 1|6 分页
			setAttr("uAnswerList", user.getAnswersPage(page.getPageSize()));
			// 获取用户的回答数
			setAttr("uAnswerCount", user.getUAnswerCount(user));
			setAttr("uTimeGo", user.getTimeGo());
			setAttr("page", page);
			render("/solution/user_answer.jsp");
		}else{
			render("/error/error.jsp");
		}
	}
	
	// 用户的问题
	public void questions(){
		User user = User.dao.findById(getParaToInt()); 		
		int pageNum = getPara("page") == null ? 1 : getParaToInt("page");
		if(StringKit.notNull(user)){
			Integer uQuestCount = User.dao.getUQuestCount(user);
			PageUtil page = new PageUtil(pageNum, uQuestCount, "u/questions/" + getPara());
			setAttr("user", user);
			// 查询用户的问题 1|6 分页 
			setAttr("uQuestList", user.getQuestPage(page.getPageSize()));
			// 查询用户的问题数 
			setAttr("uQuestCount", uQuestCount);
			setAttr("uTimeGo", user.getTimeGo());
			setAttr("page", page);
			render("/solution/user_question.jsp");
		}else{
			render("/error/error.jsp");
		}
	}

}

